package com.aube.interact.fighter;

import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.aube.interact.fighter.model.ImageItem;
import com.huyn.baseframework.dynamicload.video.model.GoodsModel;
import com.huyn.baseframework.model.ChannelDetail;
import com.huyn.baseframework.utils.UmengUtils;
import com.huyn.baseframework.utils.Utils;

/**
 * Created by hill on 10/24.
 */
public class GoodsHotpotLayout extends FrameLayout {
    private String TAG = "Aube/GoodsHotpotLayout";

    private IGoodsDetailRevealAnimator mGoodsAnimator;

    private ChannelDetail mChannel;

    private SmallGoodsItem mSmallGoodsItem = null;
    private int mSmallGoodsItem_X = 0, mSmallGoodsItem_Y = 0;

    public GoodsHotpotLayout(Context context) {
        this(context, null);
    }
    public GoodsHotpotLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public GoodsHotpotLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);

        if(mHotpotView != null && mGoodsHotpotVisible) {
            mHotpotView.layout(mHotpotX, mHotpotY, mHotpotX + mHotpotView.getMeasuredWidth(), mHotpotY + mHotpotView.getMeasuredHeight());
        }

        if(mSmallGoodsItem != null) {
            mSmallGoodsItem.layout(mSmallGoodsItem_X, mSmallGoodsItem_Y, mSmallGoodsItem.getMeasuredWidth()+ mSmallGoodsItem_X, mSmallGoodsItem.getMeasuredHeight()+ mSmallGoodsItem_Y);
        }
    }

    public void bindVideo(ChannelDetail channelDetail) {
        this.mChannel = channelDetail;
    }

    public void bindGoodsRevealAnimator(IGoodsDetailRevealAnimator animator) {
        mGoodsAnimator = animator;
    }

    private GoodsModel mData;

    private ImageItem mMiddleSizeHotpot = null;
    private int mHotpotX=0, mHotpotY=0;
    private GoodsHotpotView mHotpotView;
    public void showHotpot(GoodsModel data, final IGoodsFavorListener listener) {
        int width = getWidth();
        int height = getHeight();
        if(mMiddleSizeHotpot == null) {
            int GAP = (int) ((1 - 0.66f - 0.247f) * width / 3);

            int mWidth = (int) (width * 0.66f);
            int mHeight = (int) (width * 0.66 * 9 / 16);

            mMiddleSizeHotpot = new ImageItem(mWidth, mHeight, GAP, (height - mHeight) / 2);
        }

        if(mGoodsHotpotFullscreen) {
            mHotpotX = mHotpotY = 0;
        } else {
            mHotpotX = mMiddleSizeHotpot.x;
            mHotpotY = mMiddleSizeHotpot.y;
        }

        this.mData = data;
        LayoutParams params = mGoodsHotpotFullscreen ? new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
                : new LayoutParams(mMiddleSizeHotpot.width, mMiddleSizeHotpot.height);
        if(mHotpotView != null && mHotpotView.getParent() != null)
            removeView(mHotpotView);
        mHotpotView = new GoodsHotpotView(getContext());
        mHotpotView.setProgressListener(new GoodsHotpotView.IProgressListener() {
            @Override
            public int getCurrentProgress() {
                if(listener != null)
                    return listener.getProgress();
                return 0;
            }
        }, data.timelineCircleRespVoList);
        addView(mHotpotView, params);

        mHotpotView.setVisibility(mGoodsHotpotVisible ? VISIBLE : GONE);
        mHotpotView.setOnHotpotClickListener(new GoodsHotpotView.OnHotpotClickListener() {
            @Override
            public void onClick(int x, int y) {
                if(mChannel != null)
                    UmengUtils.reportUmengEvent(getContext(), UmengUtils.CLICK_GOODS, UmengUtils.appendLabels(mChannel.videoTitle, mChannel.videoid, mData.goodsVo.tlTitle, mData.getId()));

                if(mGoodsAnimator != null) {
                    // fill data
                    mGoodsAnimator.prepareData(mData);

                    // 2: set up view & animate it
                    int[] location = new int[2];
                    mHotpotView.getCenterLocation(location);
                    int[] args = new int[4];
                    args[0] = location[0];
                    args[1] = location[1];
                    args[2] = 1;
                    args[3] = 1;
                    mGoodsAnimator.startAnimFromCartList(args, null);
                }
            }
        });

        showGoodsThumbnail();
    }

    /**
     * 显示商品缩略图, 与热点圆圈一起出现
     */
    private void showGoodsThumbnail() {
        Utils.Log(Utils.LogType.DEBUG, TAG, "showGoodsThumbnail");

        if(mSmallGoodsItem != null && mSmallGoodsItem.getParent() != null)
            removeView(mSmallGoodsItem);

        // initialize SmallGoodsItem
        mSmallGoodsItem = new SmallGoodsItem(getContext());
        mSmallGoodsItem.fillData(mData);
        mSmallGoodsItem.addGoodsAnimListener(mGoodsAnimator);

        // setup view & its location
        mSmallGoodsItem.setVisibility(mGoodsHotpotVisible ? VISIBLE : GONE);
        mSmallGoodsItem_X = (int) (mData.getGoodsTagLeftTop_X() * getMeasuredWidth());
        mSmallGoodsItem_Y = (int) (mData.getGoodsTagLeftTop_Y() * getMeasuredHeight());
        LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        addView(mSmallGoodsItem, params);

        // finally, animate it
        mSmallGoodsItem.startAlphaAnim();
    }

    public void hideGoods() {
    }

    public boolean goodsViewRemoveable() {
        return false;
    }

    public void onDestroy() {
        if(mHotpotView != null) {
//            removeView(mHotpotView);
//            mHotpotView = null;
            mHotpotView.setVisibility(GONE);
        }

        if(mSmallGoodsItem != null) {
            mSmallGoodsItem.setVisibility(GONE);
        }
    }

    private boolean mGoodsHotpotFullscreen = true;
    private boolean mGoodsHotpotVisible = true;

    /**
     * 随着多镜头主屏幕切换尺寸
     * @param fullscreen
     */
    public void switchSize(boolean fullscreen) {
        mGoodsHotpotFullscreen = fullscreen;
        if(mHotpotView != null && mMiddleSizeHotpot != null) {
            ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f);
            anim.setDuration(300);
            anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    float value = (float) animation.getAnimatedValue();

                    ViewGroup.LayoutParams param = mHotpotView.getLayoutParams();
                    if(mGoodsHotpotFullscreen) {
                        param.width = (int) (mMiddleSizeHotpot.width + (getWidth()-mMiddleSizeHotpot.width)*value);
                        param.height = (int) (mMiddleSizeHotpot.height + (getHeight()-mMiddleSizeHotpot.height)*value);
                        mHotpotX = (int) (mMiddleSizeHotpot.x*(1-value));
                        mHotpotY = (int) (mMiddleSizeHotpot.y*(1-value));
                    } else {
                        param.width = (int) (mMiddleSizeHotpot.width + (getWidth()-mMiddleSizeHotpot.width)*(1-value));
                        param.height = (int) (mMiddleSizeHotpot.height + (getHeight()-mMiddleSizeHotpot.height)*(1-value));
                        mHotpotX = (int) (mMiddleSizeHotpot.x*(value));
                        mHotpotY = (int) (mMiddleSizeHotpot.y*(value));
                    }
                    mHotpotView.setLayoutParams(param);
                    requestLayout();
                }
            });
            anim.start();
        }
    }

    /**
     * 是否展示热点
     * @param display
     */
    public void diaplayHotpot(boolean display) {
        mGoodsHotpotVisible = display;
        if(mHotpotView != null)
            mHotpotView.setVisibility(display ? VISIBLE : GONE);
        if(mSmallGoodsItem != null)
            mSmallGoodsItem.setVisibility(display ? VISIBLE : GONE);
    }

}
